<?php

namespace common\queues;

use common\models\export\Log;
use services\baibai\ConfigService;
use Yii;
use yii\base\BaseObject;

/**
 * 导出数据
 *
 * Class ExportJob
 * @package common\queues
 */
class ExportJob extends BaseObject implements \yii\queue\JobInterface
{
    /**
     * 日志id
     *
     * @var
     */
    public $logId;

    /**
     * 文件名称和路径
     *
     * @var
     */
    public $nameAndPath;

    /**
     * 模块id
     *
     * @var
     */
    public $mudle_id;

    /**
     * 导出的字段
     *
     * @var
     */
    public $select;

    /**
     * 数据所在项目
     *
     * @var
     */
    public $project_id;

    /**
     * 导出条件
     *
     * @var
     */
    public $condition;

    /**
     * 操作人
     *
     * @var
     */
    public $handle;

    /**
     * 加入队列导出
     *
     * @param \yii\queue\Queue $queue
     * @return mixed|void
     * @throws \yii\web\HttpException
     * @author chenyuebin
     */
    public function execute($queue)
    {
        $ret = false;
//        switch ($this->mudle_id) {
//            case ConfigService::PATIENT_ID:// 患者数据
//                $ret = Yii::$app->services->patientUser->getPatientList($this->logId, $this->nameAndPath, $this->select, $this->project_id, $this->mudle_id, $this->condition);
//                break;
//            case ConfigService::MEDICAL_ID:// 导医数据
//                $ret = Yii::$app->services->patientUser->getMedicalList($this->logId, $this->nameAndPath, $this->select, $this->project_id, $this->mudle_id, $this->condition);
//                break;
//            case ConfigService::CONSUMPTION_ID:// 消费数据
//                $ret = Yii::$app->services->patientUser->getConsumptionList($this->logId, $this->nameAndPath, $this->select, $this->project_id, $this->mudle_id, $this->condition);
//                break;
//            case ConfigService::TREATMENT_ID:// 治疗数据
//                $ret = Yii::$app->services->patientUser->getTreatmentExportList($this->logId, $this->nameAndPath, $this->select, $this->project_id, $this->mudle_id, $this->condition);
//                break;
//            case ConfigService::CALLBACK_ID:// 回访数据
//                $ret = Yii::$app->services->patientUser->getCallbackList($this->logId, $this->nameAndPath, $this->select, $this->project_id, $this->mudle_id, $this->condition);
//                break;
//            default:
//                break;
//        }

        $ret = Yii::$app->services->patientUser->getPatientUserCardId($this->select, $this->condition,$this->logId,$this->nameAndPath,$this->project_id);

        if (!$ret) {
            // 导出失败
            $logParams = [
                'id' => $this->logId,
                'down_link' => '',
                'status' => Log::STATUS_FAIL,
                'success_num' => 0,
                'failed_num' => 0,
                'remark' => '数据量过大，队列超时'
            ];
            if (Yii::$app->services->exportLog->updateLog($logParams)) {
                return true;
            }
        }
    }
}